package com.wlstock.funi.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.wlstock.funi.model.FundStaticModel;
import com.wlstock.funi.model.MyAccountStocks;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.SelfFundTagModel;
import com.wlstock.funi.model.StrategyModel;
import com.wlstock.funi.model.TrackTradeModel;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


public class DataBaseHelper extends OrmLiteSqliteOpenHelper
{
	private static final String TABLE_NAME = "sqlite-test.db";

	private Map<String, Dao> daos = new HashMap<String, Dao>();

	private DataBaseHelper(Context context)
	{
		super(context, TABLE_NAME, null, DBVersionUtility.newVersion);
	}

	@Override
	public void onCreate(SQLiteDatabase database,
			ConnectionSource connectionSource)
	{
		try
		{
			TableUtils.createTable(connectionSource, OperateModel.class);	
			TableUtils.createTable(connectionSource, TrackTradeModel.class);
			TableUtils.createTable(connectionSource, FundStaticModel.class);
			TableUtils.createTable(connectionSource, MyAccountStocks.class);
			TableUtils.createTable(connectionSource, SelfFundTagModel.class);
			TableUtils.createTable(connectionSource, StrategyModel.class);
			
			updateAllVersion(DBVersionUtility.newVersion,connectionSource);

		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database,
			ConnectionSource connectionSource, int oldVersion, int newVersion) {
		updateVersions(oldVersion,newVersion,connectionSource);
	}


	private void updateToVersion(int version,ConnectionSource connectionSource){
		try {
			Class<?> updateClass  = Class.forName(UpdateTableUtils.class.getName());
			Method updateMethod = updateClass.getMethod("updateV" + version, ConnectionSource.class);
			updateMethod.invoke(null, connectionSource);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	private  void updateAllVersion(int lastVersion,ConnectionSource connectionSource){
		updateVersions(1,lastVersion,connectionSource);
	}

	/**
	 * 新版本是V16,则调用的是UpdateV15以及之前的所有方法
	 * @param oldVersion
	 * @param lastVersion
	 * @param connectionSource
     */
	private void  updateVersions(int oldVersion, int lastVersion,ConnectionSource connectionSource){
		for (int i = oldVersion; i <lastVersion; i++){
			updateToVersion(i ,connectionSource);
		}
	}

	private static DataBaseHelper instance;

	/**
	 * 单例获取该Helper
	 * 
	 * @param context
	 * @return
	 */
	public static synchronized DataBaseHelper getHelper(Context context)
	{
		context = context.getApplicationContext();
		if (instance == null)
		{
			synchronized (DataBaseHelper.class)
			{
				if (instance == null)
					instance = new DataBaseHelper(context);
			}
		}

		return instance;
	}

	public synchronized Dao getDao(Class clazz) throws SQLException
	{
		Dao dao = null;
		String className = clazz.getSimpleName();

		if (daos.containsKey(className))
		{
			dao = daos.get(className);
		}
		if (dao == null)
		{
			dao = super.getDao(clazz);
			daos.put(className, dao);
		}
		return dao;
	}

	/**
	 * 释放资源
	 */
	@Override
	public void close()
	{
		super.close();

		for (String key : daos.keySet())
		{
			Dao dao = daos.get(key);
			dao = null;
		}
	}
	
	
	
}

